package fch;

import static org.junit.Assert.assertFalse;
import robocode.Rules;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IBulletSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * Test that the CrazyTracker robot only fires the expected bullet powers.
 * @author Christopher Foo
 *
 */
public class TestFiring extends RobotTestBed {
  
  /** True if an unexpected bullet power is found. */
  private boolean invalidPowerFound = false;
  
  @Override
  /**
   * Check to see if the any bullets with powers different from the ones specified in the
   * CrazyTracker's calculatePower method are found.
   * 
   * @param event Details about the finished turn.
   */
  public void onTurnEnded(TurnEndedEvent event) {
    IBulletSnapshot[] bullets = event.getTurnSnapshot().getBullets();
    for (int i = 0; i < bullets.length; i++) {
      if (Math.abs(bullets[i].getPower() - Rules.MAX_BULLET_POWER) >= 0.01  
         && Math.abs(bullets[i].getPower() - (Rules.MAX_BULLET_POWER * 2 ) / 3) >= 0.01
         && Math.abs(bullets[i].getPower() - Rules.MAX_BULLET_POWER / 3 ) >= 0.01) {
        invalidPowerFound = true;
      }
    }
  }
  
  /**
   * After the battle, check to see that no invalid bullet powers were found.
   *
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertFalse("Check for invalid bullet powers", invalidPowerFound);
  }
  
  @Override
  /**
   * Use SittingDuck and the CrazyTracker for this test.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  public String getRobotNames() {
    return "sample.SittingDuck,fch.CrazyTracker";
  }

  /**
   * This test runs for 10 round.
   *
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

}
